package com.example;

/**
 * @author yeqiang
 * @since 2021/8/18 下午1:46
 * https://zhuanlan.zhihu.com/p/337375554
 */
public class OrderRearrangeDemo {
    static boolean initFlag;

    public static void main(String... args) {
        while (true) {
            OrderRearrangeDemo demo = new OrderRearrangeDemo();
            demo.startDemo();
        }
    }

    void startDemo() {
        Runnable customRunnable = new CustomRunnable();
        new Thread(customRunnable, "Thread-01").start();
        new Thread(customRunnable, "Thread-02").start();
    }

    static class CustomRunnable implements Runnable {
        @Override
        public void run() {
            initFlag = false;
            Integer number = null;
            number = 1;
            initFlag = true;
            // 等待初始化完成
            while (!initFlag) {
            }
            if (number == null) {
                System.out.println("name: " + Thread.currentThread().getName() + ", number: " + number);
                System.exit(1);
            }
        }
    }
}
